package templates;

import common.Application;
import utils.GenericPair;

public class PropertyTemplate {
    private short uid;
    private static final byte PRIMARY_KEY_MASK = 0x01;
    private static final byte FOREIGN_KEY_MASK = 0x02;
    private static final byte NULL_VALUE_MASK = 0x04;
    private String name;
    private String type;
    private String nullValue;
    private String externalTable;
    private byte attr;
    private Iterable<GenericPair<String, String>> params;

    public PropertyTemplate(short uid, String name, String type, String nullValue,String externalTable, byte attr, Iterable<GenericPair<String, String>> params) {
        this.uid = uid;
        this.name = name;
        this.type = type;
        this.nullValue = nullValue;
        this.attr = attr;
        this.params = params;
        this.externalTable=externalTable;
    }

    public short getUid() {
        return uid;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getNullValue() {
        return nullValue;
    }

    public String getExternalTable(){
        return externalTable;
    }

    public boolean isPrimaryKey() {
        return (attr & PRIMARY_KEY_MASK) == 0;
    }

    public boolean isForeignKey() {
        return (attr & FOREIGN_KEY_MASK) == 0;
    }

    public Boolean isNullEnabled() {
        return (attr & NULL_VALUE_MASK) == 0;
    }

    public Iterable<GenericPair<String, String>> getParams() {
        return params;
    }

    public GenericPair<String, String> getParam(String paramName) {
        for (GenericPair<String, String> a:params){
            if (a.getFirst().equals(paramName))return a;
        }
        return null;
    }

    public short getExternalUid() {
        short result = 0;
        for (Template t: Application.getInstance().getTemplateCollection()) {
            if (t.getAlias().equals(externalTable)) {
                result = t.getUid();
                break;
            }
        }
        return result;
    }

}
